package com.wangshuo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshuo.common.JwtHelper;
import com.wangshuo.config.Result;
import com.wangshuo.entity.Comment;
import com.wangshuo.entity.CommentItem;
import com.wangshuo.service.CommentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private JwtHelper jwtHelper;

    @PostMapping("/add")
    public Result add(@RequestBody Comment comment, HttpServletRequest httpServletRequest) {
        Long uid = jwtHelper.getUserId(httpServletRequest.getHeader("token"));
        comment.setUid(Math.toIntExact(uid));
        comment.setCreateTime(LocalDateTime.now());
        commentService.saveOrUpdate(comment);
        return Result.ok("成功");
    }
    @GetMapping("/list")
    public Result list(@RequestParam Integer gid){
        List<CommentItem> list=commentService.getlist(gid);
        return Result.ok(list);
    }
    @GetMapping("/allist")
    public Result alllist(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String keyword){
        IPage<CommentItem> page=new Page<>(pageNum,pageSize);
        IPage<CommentItem> list=commentService.getlist1(page,keyword);
return Result.ok(list);

    }
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        commentService.removeById(id);
        return Result.ok("成功");
    }
}
